const state = {
	address: JSON.parse(uni.getStorageSync('address') || '{}'),
	token: uni.getStorageSync('token') || '',
	userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
	redirectInfo: null // 重定向的 object 对象 { openType, from }
}

const mutations = {

	// 更新收货地址
	UPDATEADDRESS(state, address) {
		state.address = address
		this.commit('m_user/saveAddressToStorage')
	},

	// 定义将 address 持久化存储到本地的方法
	saveAddressToStorage(state) {
		uni.setStorageSync('address', JSON.stringify(state.address))
	},

	// 更新用户的信息
	UPDATEUSERINFO(state, userinfo) {
		state.userinfo = userinfo
		this.commit('m_user/saveUserInfoToStorage')
	},
	// 将 userinfo 持久化存储到本地
	saveUserInfoToStorage(state) {
		uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
	},

	// 更新 token 字符串
	UPDATETOKEN(state, token) {
		state.token = token
		this.commit('m_user/saveTokenToStorage')
	},

	// 更新重定向的信息对象
	UPDATEREDIRECTINFO(state, info) {
		state.redirectInfo = info
	},

	// 将 token 字符串持久化存储到本地
	saveTokenToStorage(state) {
		uni.setStorageSync('token', state.token)
	}
}

const actions = {
	updateAddress({
		commit
	}, address) {
		commit('UPDATEADDRESS', address)
	},

	// 更新用户的信息
	updateUserInfo({
		commit
	}, userinfo) {
		commit('UPDATEUSERINFO', userinfo)
	},

	// 更新 token 字符串
	updateToken({
		commit
	}, token) {
		commit('UPDATETOKEN', token)
	},

	updateRedirectInfo({
		commit
	}, info) {
		commit('UPDATEREDIRECTINFO', info)
	}
}

const getters = {
	addStr(state) {
		if (!state.address.provinceName) return ''
		return state.address.provinceName + state.address.cityName + state.address.countyName + state.address
			.detailInfo
	}
}


export default {
	// 为当前模块开启命名空间
	namespaced: true,
	state,
	mutations,
	actions,
	getters
}
